﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Profile;
using COFE.Core;
using System.Xml.Linq;

namespace COFE.Web
{
    /// <summary>
    /// Represents a Volume information, it's id, factory id, construct params and permission.
    /// </summary>
    public class VolumeInfo
    {
        #region Constructor

        public VolumeInfo(IVolume volume, VolumeAccessMode accessMode)
        {
            _factoryId = volume.FactoryKey;
            _constructParam = volume.GetArgs();
            _volumeid = volume.ListerKey;
            _volAccessMode = accessMode;
        }

        public VolumeInfo(IVolume volume)
            : this(volume, volume.AccessMode)
        {
        }

        private VolumeInfo()
        {

        }

        #endregion

        #region Methods

        /// Serialize the link object to xml.
        /// </summary>
        /// <returns></returns>
        public XElement ToXml()
        {
            return new XElement(XNames.VolumeInfo,
                                 new XAttribute(XNames.VolumeId, _volumeid),
                                 new XElement(XNames.FactoryId, _factoryId),
                                 new XElement(XNames.ConstructParam, _constructParam),
                                 new XElement(XNames.VolumeAccessMode, _volAccessMode.ToString()));
        }

        /// <summary>
        /// Deserialize xml to VolumeInfo object.
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="itemXml"></param>
        /// <returns></returns>
        public VolumeInfo FromXml(XElement ele)
        {
            VolumeInfo retVal = new VolumeInfo();
            retVal.VolumeId = ele.Attribute(XNames.VolumeId).Value;
            retVal.FactoryId = ele.Attribute(XNames.FactoryId).Value;
            retVal.ConstructParam = ele.Attribute(XNames.ConstructParam).Value;
            retVal.VolumeAccessMode =
                (VolumeAccessMode)Enum.Parse(typeof(VolumeAccessMode), ele.Attribute(XNames.VolumeAccessMode).Value);

            return retVal;
        }
       

        #endregion

        #region Data

        string _factoryId, _volumeid, _constructParam;        
        VolumeAccessMode _volAccessMode;

        #endregion

        #region Public Properties

        /// <summary>
        /// Constructor id of this Volume.
        /// </summary>
        public string FactoryId { get { return _factoryId; } private set { _factoryId = value; } }

        /// <summary>
        /// Parameter used to construct the volume.
        /// </summary>
        public string ConstructParam { get { return _volumeid; } private set { _volumeid = value; } }

        /// <summary>
        /// Id of this volume.
        /// </summary>
        public string VolumeId { get { return _constructParam; } private set { _constructParam = value; } }

        /// <summary>
        /// Default or current volume access mode.
        /// </summary>
        public VolumeAccessMode VolumeAccessMode { get { return _volAccessMode; } private set { _volAccessMode = value; } }

        #endregion
    }
}
